/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7


import "DCDataClasses.js" as DCDATA

Dialog {
    id: carpool
    width: 600
    height: 400
    label:  "CarPool"
    color: "#5d8eac"

    property string datacloudurl: ""
    property string username: ""
    property string password: ""
    property string karpoid: ""
    property string useralias: ""

    property string currentcarpoolcarpoid:"0"
    property int currentcarpoolindex: 0
    property string currentcarpoolstate:"none" // "modify" "new"

    signal startSendingGPS(string carpoolkarpoid, string useralias)
    signal stopSendingGPS()


    function setSelectedCarpool(label)
    {
        for (var i = 0; i < carpoolmodel.count; i++)
        {

            if (carpoolmodel.get(i).name == label)
            {
                currentcarpoolindex = i;

                carpoolcombobox.setSelectedItem(label)

                currentcarpoolcarpoid = carpoolmodel.get(currentcarpoolindex).carpoolkarpoid
                carpoolnameinput.text = carpoolmodel.get(currentcarpoolindex).name
                carpooldescriptioninput.text = carpoolmodel.get(currentcarpoolindex).description

                var emptyarray = []
                carpoolroutenameid.text = "<not set>"
                carpoolmemberscombobox.setComboItems(emptyarray)
                carpoolmemberscombobox.setSelectedItem("")

                try
                {
                    carpoolroutenameid.text = carpoolmodel.get(currentcarpoolindex).routename
                }
                catch (e)
                {
                    console.log("setSelectedCarpool: exception: " + e + "==> " + carpoolmodel.get(currentcarpoolindex).routes)
                }

                try
                {
                    var members = JSON.parse(carpoolmodel.get(currentcarpoolindex).members)
                    if (typeof members == 'object')
                    {
                        var membersarray = []
                        for (var m=0;m<members.length;m++)
                        {
                            membersarray[m] = members[m].lastname + ", " + members[m].firstname + " [" + members[m].status + "]"
                        }
                        carpoolmemberscombobox.setComboItems(membersarray)
                        if (membersarray.length > 0)
                        {
                            carpoolmemberscombobox.setSelectedItem(membersarray[0])
                        }
                    }
                }
                catch (e)
                {
                    console.log("MEMBERS ex: " + e)
                }


                modifycarpoolbutton.setEnabled(true)
                newcarpoolbutton.setEnabled(true)
                savecarpoolbutton.setEnabled(false)
                cancelcarpoolbutton.setEnabled(false)

                // stop the for-loop
                i = carpoolmodel.count

            }
        }

    }



    property int labelfontpixesize: 14
    property int labeltopmargin: 12
    property int inputboxheight: 26
    property int inputboxtopmargin: 2
    property int labelWidth: 90

    Rectangle
    {

        id: carpoolsrect
        color: "#00000000"
        height: carpool.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: carpool.titlerect.bottom
        anchors.topMargin: 5
        z: 3

        Text {
            id: carpoolid
            width: labelWidth
            text: "CarPools"
            anchors.left: carpoolsrect.left
            anchors.leftMargin: 10
            anchors.verticalCenter: carpoolsrect.verticalCenter
            font.pixelSize: 0
        }

        ComboBox {
            id: carpoolcombobox
            height: carpool.inputboxheight
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: carpoolid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom


            onItemDelete:
            {
                if (label == "")
                {
                    messagebox.text = "Nothing selected"
                    messagebox.show("")
                }
                else
                {
                    messagebox.text = "Do you want to delete<br>" + label
                    messagebox.onOkTodo = "deleteSelectedCarpool"
                    messagebox.show("confirmation")
                }
            }

            onItemSelected:
            {
                setSelectedCarpool(label)
            }

        }
    }


    Rectangle
    {
        id: carpoolcontentrect
        color: "#00000000"
        border.color: "#253244"
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.top: carpoolsrect.bottom
        anchors.topMargin: 5
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10

        Text {
            id: carpoolpropertiesid
            text: "Properties of the selected CarPool:"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.top: parent.top
            anchors.topMargin: 5
            font.pixelSize: carpool.labelfontpixesize
        }


        Rectangle
        {
            id: namerect
            color: "#00000000"
            height: carpool.inputboxheight
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: carpoolpropertiesid.bottom
            anchors.topMargin: carpool.inputboxtopmargin

            Text {
                id: carpoolnameid
                width: carpool.labelWidth
                text: "Name"
                anchors.left: parent.left
                anchors.leftMargin: 10
                anchors.verticalCenter: namerect.verticalCenter
                font.pixelSize: labelfontpixesize
            }

            Rectangle {
                id: carpoolnameinputrectangle
                height: carpool.inputboxheight
                color: "#ffffff"
                anchors.right: parent.right
                anchors.rightMargin: 10
                anchors.left: carpoolnameid.right
                anchors.leftMargin: 0
                anchors.top: parent.top
                anchors.bottom: parent.bottom

                border.color: "#000901"

                TextInput {
                    id: carpoolnameinput
                    text: ""
                    anchors.leftMargin: 3
                    anchors.fill: parent
                    selectionColor: "#316ac5"
                    font.pixelSize: carpool.labelfontpixesize
                }
            }
        }


        Rectangle
        {
            id: descriptionrect
            color: "#00000000"
            height: carpool.inputboxheight
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: namerect.bottom
            anchors.topMargin: carpool.inputboxtopmargin

            Text {
                id: carpooldescriptionid
                width: carpool.labelWidth
                text: "Description"
                anchors.left: parent.left
                anchors.leftMargin: 10
                anchors.verticalCenter: descriptionrect.verticalCenter
                font.pixelSize: carpool.labelfontpixesize
            }

            Rectangle {
                id: carpooldescriptioninputrectangle
                height: carpool.inputboxheight
                color: "#ffffff"
                anchors.right: parent.right
                anchors.rightMargin: 10
                anchors.left: carpooldescriptionid.right
                anchors.leftMargin: 0
                anchors.top: parent.top
                anchors.bottom: parent.bottom
                border.color: "#000901"

                TextInput {
                    id: carpooldescriptioninput
                    text: ""
                    anchors.leftMargin: 3
                    anchors.fill: parent
                    selectionColor: "#316ac5"
                    font.pixelSize: carpool.labelfontpixesize
                }
            }

        }

        Rectangle
        {
            id: routerect
            color: "#00000000"
            height: carpool.inputboxheight
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: descriptionrect.bottom
            anchors.topMargin: carpool.inputboxtopmargin
            z: 4

            Text {
                id: carpoolrouteid
                width: carpool.labelWidth
                text: "Route"
                anchors.left: parent.left
                anchors.leftMargin: 10
                anchors.verticalCenter: routerect.verticalCenter
                font.pixelSize: carpool.labelfontpixesize
            }


            Text {
                id: carpoolroutenameid
                width: carpool.labelWidth
                text: "<not set>"
                anchors.right: parent.right
                anchors.rightMargin: 10
                anchors.left: carpoolrouteid.right
                anchors.leftMargin: 0
                anchors.verticalCenter: routerect.verticalCenter
                font.pixelSize: carpool.labelfontpixesize
            }

        }

        Rectangle
        {
            id: membersrect
            color: "#00000000"
            height: carpool.inputboxheight
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: routerect.bottom
            anchors.topMargin: carpool.inputboxtopmargin
            z: 3

            Text {
                id: carpoolmembersid
                width: carpool.labelWidth
                text: "Members"
                anchors.left: parent.left
                anchors.leftMargin: 10
                anchors.verticalCenter: membersrect.verticalCenter
                font.pixelSize: carpool.labelfontpixesize
            }

            ComboBox {
                id: carpoolmemberscombobox
                height: carpool.inputboxheight
                anchors.right: parent.right
                anchors.rightMargin: 10
                anchors.left: carpoolmembersid.right
                anchors.leftMargin: 0
                anchors.top: parent.top
                anchors.bottom: parent.bottom

                onItemDelete:
                {
                    if (label == "")
                    {
                        messagebox.text = "Nothing selected"
                        messagebox.show("")
                    }
                    else
                    {
                        messagebox.text = "Do you want to delete<br>" + label
                        messagebox.onOkTodo = "deleteSelectedMember"
                        messagebox.show("confirmation")
                    }
                }

            }
        }

        PushButton
        {
            id: cancelcarpoolbutton
            width: 75
            label: "Cancel"
            anchors.right: parent.right
            anchors.rightMargin: 5
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 5

            onButtonpressed:
            {
                currentcarpoolcarpoid = "0"
                carpoolnameinput.text = ""
                carpooldescriptioninput.text = ""
                var emptyarray = []
                carpoolroutenameid.text = "<not set>"
                carpoolmemberscombobox.setComboItems(emptyarray)
                carpoolcombobox.setSelectedItem("")
                carpoolmemberscombobox.setSelectedItem("")
                savecarpoolbutton.setEnabled(false)
                cancelcarpoolbutton.setEnabled(false)
                newcarpoolbutton.setEnabled(true)
                modifycarpoolbutton.setEnabled(false)
                currentcarpoolstate = "none"
            }

        }

        Timer {
            id: getsavedcarpoolstimer
            interval: 2000
            running: false
            repeat: false
            onTriggered:
            {
                var carpoollist = new DCDATA.DCCarpoolList();
                carpoollist.karpoid = carpool.karpoid
                carpoollist.password = carpool.password

                DCDATA.send(carpoollist, carpool.datacloudurl, "getcarpoolsbyowner", getcarpoolscb)
            }
         }

        Timer {
            id: initandshowtimer
            interval: 2000
            running: false
            repeat: false
            onTriggered:
            {
                initAndShow();
            }
         }

        function savecarpoolcb(response)
        {
            console.log("savecarpoolcb");
            // udpate the carpool list
            // First wait 2 secs so that the server has time to create and save the new carpool
            getsavedcarpoolstimer.start();

        }


        PushButton
        {
            id: savecarpoolbutton
            width: 75
            label: "Save"
            anchors.right: cancelcarpoolbutton.left
            anchors.rightMargin: 5
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 5

            onButtonpressed:
            {
                if (carpoolnameinput.text == "")
                {
                    messagebox.text = "Name is missing!"
                    messagebox.show("")
                }
                else
                {
                    savecarpoolbutton.setEnabled(false)
                    cancelcarpoolbutton.setEnabled(false)


                    messagebox.text = "Saving the carpool ..."
                    messagebox.show("")

                    if (currentcarpoolstate == "new")
                    {
                        var newcp = new DCDATA.DCCarpool();
                        newcp.karpoid = karpoid
                        newcp.carpoolkarpoid = "" // generated in the server sid
                        newcp.ownerkarpoid = karpoid
                        newcp.name = carpoolnameinput.text
                        newcp.description = carpooldescriptioninput.text
                        newcp.routekarpoid = "0"
                        newcp.routename = "<not set>"
                        newcp.members = []

                        var dccarpoollist = new DCDATA.DCCarpoolList();
                        dccarpoollist.username = username
                        dccarpoollist.password = password
                        dccarpoollist.karpoid = karpoid
                        for (var cp = 0; cp < carpoolmodel.count; cp++)
                        {
                            var carp = carpoolmodel.get(cp);
                            carp.members = JSON.parse(carp.members)
                            dccarpoollist.carpoollist[cp] = carp
                        }
                        dccarpoollist.carpoollist[carpoolmodel.count] = newcp

                        DCDATA.send(dccarpoollist, datacloudurl, "savecarpools", getcarpoolscb)
                    }
                    else if (currentcarpoolstate == "modify")
                    {
                        var modifiedcp = carpoolmodel.get(currentcarpoolindex);
                        modifiedcp.members = JSON.parse(modifiedcp.members)
                        modifiedcp.name = carpoolnameinput.text
                        modifiedcp.description = carpooldescriptioninput.text

                        var dccarpoollist = new DCDATA.DCCarpoolList();
                        dccarpoollist.username = username
                        dccarpoollist.password = password
                        dccarpoollist.karpoid = karpoid
                        for (var cp = 0; cp < carpoolmodel.count; cp++)
                        {
                            if (cp != currentcarpoolindex)
                            {
                                var carp = carpoolmodel.get(cp);
                                carp.members = JSON.parse(carp.members)
                                dccarpoollist.carpoollist[cp] = carp
                            }
                            else
                            {
                                dccarpoollist.carpoollist[cp] = modifiedcp
                            }
                        }

                        DCDATA.send(dccarpoollist, datacloudurl, "savecarpools", getcarpoolscb)

                    }
                    currentcarpoolstate = "none"
                }
            }
        }



        PushButton
        {
            id: newcarpoolbutton
            width: 75
            label: "New"
            anchors.right: savecarpoolbutton.left
            anchors.rightMargin: 5
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 5

            onButtonpressed:
            {
                currentcarpoolcarpoid = "0"
                carpoolnameinput.text = ""
                carpooldescriptioninput.text = ""
                var emptyarray = []
                carpoolroutenameid.text = "<not set>"
                carpoolmemberscombobox.setComboItems(emptyarray)
                carpoolcombobox.setSelectedItem("")
                carpoolmemberscombobox.setSelectedItem("")
                savecarpoolbutton.setEnabled(true)
                cancelcarpoolbutton.setEnabled(true)
                modifycarpoolbutton.setEnabled(false)
                newcarpoolbutton.setEnabled(false)
                currentcarpoolstate = "new"
            }

        }

        PushButton
        {
            id: modifycarpoolbutton
            width: 75
            label: "Modify"
            anchors.right: newcarpoolbutton.left
            anchors.rightMargin: 5
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 5

            onButtonpressed:
            {
                savecarpoolbutton.setEnabled(true)
                cancelcarpoolbutton.setEnabled(true)
                newcarpoolbutton.setEnabled(false)
                modifycarpoolbutton.setEnabled(false)
                currentcarpoolstate = "modify"
            }

        }
    }

    onMenuSelection:
    {
        if (selectionlabel == "Back...")
        {
            carpool.visible = false
            cancel()
        }
        else
        {
            if (currentcarpoolstate == "none" && currentcarpoolcarpoid == "0")
            {
                messagebox.text = "You don't have a carpool selected!"
                messagebox.show("")
            }
            else
            {
                if (selectionlabel == "Add members...")
                {
                    if (currentcarpoolcarpoid == "0")
                    {
                        messagebox.text = "Before adding members<br>do save the new carpool!"
                        messagebox.show("")
                    }
                    else
                    {
                        memberselectionid.visible = true
                     }
                }
                else if (selectionlabel == "Send invitations...")
                {
                    invitationdlg.setMembersToInvite(carpoolmodel.get(currentcarpoolindex).members)
                    invitationdlg.x = (carpool.width - invitationdlg.width)/2
                    invitationdlg.y = titlerect.height
                    invitationdlg.visible = true
                }
                else if (selectionlabel == "Send message...")
                {
                    if (currentcarpoolcarpoid == "")
                    {
                        messagebox.text = "Activate some carpool first" ;
                        messagebox.show("")
                    }
                    else
                    {
                        // find out the carpool's members' karpoids
                        var currentcarpoolmembers = JSON.parse(carpoolmodel.get(currentcarpoolindex).members)
                        var memberkarpoidarray = []

                        var c = 0
                        for (var m=0;m<currentcarpoolmembers.length;m++)
                        {
                            if (currentcarpoolmembers[m].status == "accepted")
                            {
                                memberkarpoidarray[c] = currentcarpoolmembers[m].memberkarpoid
                                c++
                            }
                        }

                        messagesendingwin.show(currentcarpoolcarpoid,memberkarpoidarray)
                    }
                }
                else if (selectionlabel == "Activate...")
                {

                    carpoolactivation.initAndShow(useralias, currentcarpoolcarpoid)
                }
            }
         }
    }

    CarpoolActivation
    {
        id: carpoolactivation
        anchors.centerIn: parent
        datacloudurl: carpool.datacloudurl
        visible: false

        onStartSendingGPS:
        {
            console.log(" START SENDING GPS for carpoolkarpoid=" + carpoolkarpoid)

            carpool.startSendingGPS(carpoolkarpoid, showalias)
        }

        onStopSendingGPS:
        {
            console.log(" STOP SENDING GPS")
            carpool.stopSendingGPS()
        }
    }

    MessageSending
    {
        id: messagesendingwin;
        width: carpool.width
        height: carpool.height - carpool.titlerect.height
        x: 0
        y: carpool.titlerect.height
        datacloudurl: carpool.datacloudurl
        karpoid: carpool.karpoid
        username: carpool.username
        password: carpool.password

    }

    ListModel {
        id: carpoolmodel
    }



    function getcarpoolscb(response)
    {
        console.log("getcarpoolscb: count = " + response.carpoollist.length)
        carpoolmodel.clear()

        var cparray = []
        if (response.carpoollist.length > 0)
        {
            for (var i = 0; i < response.carpoollist.length; i++)
            {
                // Workaround: Change the members array to json string because
                // if not changed then the content of the array is somehow lost
                // when added in the ListModel
                var carpoolobj = response.carpoollist[i]
                var membersasjson = JSON.stringify(carpoolobj.members)
                carpoolobj.members = membersasjson
                carpoolmodel.append(carpoolobj)

                cparray[i] = response.carpoollist[i].name

            }
        }

        carpoolcombobox.setComboItems(cparray);

        newcarpoolbutton.setEnabled(true)

        if (messagebox.visible)
        {
            messagebox.visible = false
        }

        // saved carpool is now in the carpool combobox
        // set it seleected
        if (carpoolnameinput.text.length > 0)
        {
            carpoolcombobox.setSelectedItem(carpoolnameinput.text)
        }

        if (carpoolcombobox.selectedind > -1)
        {
            setSelectedCarpool(carpoolcombobox.getSelectedLabel())
        }
        else if (carpoolmodel.count > 0)
        {

        }

        messagebox.visible = false

    }

    function initAndShow()
    {
        // get the save carpools
        messagebox.text = "Loading saved carpools..."
        messagebox.show("")

        currentcarpoolcarpoid = "0"
        carpoolnameinput.text = ""
        carpooldescriptioninput.text = ""
        var emptyarray = []
        carpoolroutenameid.text = "<not set>"
        carpoolmemberscombobox.setComboItems(emptyarray)
        carpoolcombobox.setSelectedItem("")
        carpoolmemberscombobox.setSelectedItem("")
        savecarpoolbutton.setEnabled(false)
        cancelcarpoolbutton.setEnabled(false)
        newcarpoolbutton.setEnabled(true)
        modifycarpoolbutton.setEnabled(false)
        currentcarpoolstate = "none"


        var carpoollist = new DCDATA.DCCarpoolList();
        carpoollist.karpoid = carpool.karpoid
        carpoollist.password = carpool.password

        DCDATA.send(carpoollist, carpool.datacloudurl, "getcarpoolsbyowner", getcarpoolscb)

        visible = true

    }

    function deleteSelectedCarpool()
    {
        console.log("deleteSelectedCarpool: " + carpoolcombobox.getSelectedLabel())
        messagebox.text = "Deleting selected carpool..."
        messagebox.show("")

        var carpoollist = new DCDATA.DCCarpoolList();
        carpoollist.karpoid = carpool.karpoid
        carpoollist.password = carpool.password
        carpoollist.carpoollist[0] = carpoolmodel.get(currentcarpoolindex)
        carpoollist.carpoollist[0].members = JSON.parse(carpoollist.carpoollist[0].members);

        DCDATA.send(carpoollist, carpool.datacloudurl, "deletecarpool", deletecarpoolcb)
    }

    function deletecarpoolcb(result)
    {
        console.log("deletecarpoolcb: " + result.statusString)

        if (result.isError)
        {
            messagebox.text = "Failed. " + result.errorString
            messagebox.show("")
        }
        else
        {
            // Load the saved carpools again
            // First wait 2 secs so that the server has time to create and save the new carpool
            initandshowtimer.start();

        }
    }

    property string deletedmembers: "[]"

    function deletemembercb(result)
    {

        console.log("deletemembercb: " + result.statusString)

        if (result.isError)
        {
            messagebox.text = "Failed. " + result.errorString
            messagebox.show("")
        }
        else
        {
            // update the member list
            var currentcarpoolmembers = JSON.parse(carpoolmodel.get(currentcarpoolindex).members)
            var selectedmemberid = carpoolmemberscombobox.selectedind

            carpoolmemberscombobox.deleteComboItem(carpoolmemberscombobox.selectedind)

            var temparray = []
            var m2 = 0;
            for (var m=0;m<currentcarpoolmembers.length;m++)
            {
                if (m != selectedmemberid)
                {
                    temparray[m2] = currentcarpoolmembers[m]
                    m2++;
                }
            }

            carpoolmodel.get(currentcarpoolindex).members = JSON.stringify(temparray)

            messagebox.text = "Member deleted"
            messagebox.show("")

        }
    }


    function deleteSelectedMember()
    {

        var currentcarpoolmembers = JSON.parse(carpoolmodel.get(currentcarpoolindex).members)

        var karpoids = []
        karpoids[0] = currentcarpoolmembers[carpoolmemberscombobox.selectedind].carpoolmemberkarpoid;

        var dckarpoidlist = new DCDATA.DCKarpoIdList();
        dckarpoidlist.username = username
        dckarpoidlist.karpoid = karpoid
        dckarpoidlist.password = password
        dckarpoidlist.karpoids = karpoids

        DCDATA.send(dckarpoidlist,datacloudurl, "deletecarpoolmembers", deletemembercb)

    }

    MessageBox
    {
        id: messagebox
        property string onOkTodo: "" // deleteSelectedCarpool | deleteSelectedRoute | deleteSelectedMember

        onOk:
        {
            if (onOkTodo == "deleteSelectedCarpool")
            {
                deleteSelectedCarpool()
            }
            else if (onOkTodo == "deleteSelectedMember")
            {
                deleteSelectedMember()
            }
            onOkTodo = ""
        }

        onCancel:
        {
            onOkTodo = ""
        }


    }

    Component.onCompleted:
    {


        var arr = ["Add members...","Send invitations...","Send message...","Activate...","Back..."]
        carpool.addMenuItems(arr,false)

        savecarpoolbutton.setEnabled(false)
        cancelcarpoolbutton.setEnabled(false)
        modifycarpoolbutton.setEnabled(false)

    }



    MemberSelection
    {
        id: memberselectionid
        anchors.fill: parent
        datacloudurl: carpool.datacloudurl
        ownerkarpoid: carpool.karpoid
        username: carpool.username
        password: carpool.password
        carpoolkarpoid: carpool.currentcarpoolcarpoid
        messages: carpool.messages

        visible: false

        function addcarpoolmemberscb(result)
        {
            console.log("addcarpoolmemberscb result = " + result)
            getsavedcarpoolstimer.start();
        }

        onMembersSelected:
        {
            var memlist = JSON.parse(memberslist)

            memberselectionid.visible = false
            messagebox.text = "Saving the memberslist..."
            messagebox.visible = true

            var members = []
            for (var m = 0; m < memlist.length; m++)
            {
                var mem = new DCDATA.DCCarpoolMember()
                mem.carpoolmemberkarpoid = "" // generated in the server side
                mem.carpoolkarpoid = currentcarpoolcarpoid
                mem.ownerkarpoid = karpoid
                mem.memberkarpoid = memlist[m]["karpoid"]
                mem.firstname = memlist[m]["firstname"]
                mem.lastname = memlist[m]["lastname"]
                mem.phone = memlist[m]["phone"]
                mem.hometown = memlist[m]["hometown"]
                mem.status = "uninvited"
                mem.picklat = "0"
                mem.picklon = "0"

                members[m] = mem
            }


            var dcmemberlist = new DCDATA.DCCarpoolMembersList()
            dcmemberlist.username = username
            dcmemberlist.password = password
            dcmemberlist.karpoid = karpoid
            dcmemberlist.members = members

            DCDATA.send(dcmemberlist, datacloudurl, "addcarpoolmembers", addcarpoolmemberscb)

        }

        onCancel:
        {
            memberselectionid.visible = false
        }
    }


    InvitationSending
    {
        id: invitationdlg

        datacloudurl: carpool.datacloudurl
        username: carpool.username
        password: carpool.password
        karpoid: carpool.karpoid
        carpoolkarpoid: currentcarpoolcarpoid

        visible: false

        onInvitedMembers:
        {

            // update the invited status from uninvited to invited
            var dccarpoollist = new DCDATA.DCCarpoolList();
            dccarpoollist.username = username
            dccarpoollist.password = password
            dccarpoollist.karpoid = karpoid
            for (var cp = 0; cp < carpoolmodel.count; cp++)
            {
                var carp = carpoolmodel.get(cp);
                var carpmembers = JSON.parse(carp.members)

                for (var i=0;i<memberlist.length;i++)
                {
                    for (var m = 0; m < carpmembers.length; m++)
                    {
                        if (carpmembers[m].carpoolmemberkarpoid == memberlist[i])
                        {
                            carpmembers[m].status = "invited"
                        }
                    }
                }

                carp.members = carpmembers
                dccarpoollist.carpoollist[cp] = carp
            }

            DCDATA.send(dccarpoollist, datacloudurl, "savecarpools", getcarpoolscb)

        }

    }
}


